<?php
	$p = $_GET['p'];
	$file = ($p==8?'p_tools':($p==7?'':($p==6?'p_daysay':($p==5?'':($p==4?'':($p==3?'p_about':($p==2?'p_tage':($p==1?'p_focus':'index')))))))).'.html';

	if(file_exists('../../one/xml/index-data.xml'))
	{
		$xml = simplexml_load_file('../../one/xml/index-data.xml');
	}
	$xml = objectToArray($xml);

	switch($p)
	{
		case 1:
			$key = 'Foucs';
			$xml[$key] = $xml[$key] ? $xml[$key] : array('','','','','');			
			break;
		case 2:
			$key = 'Tages';
			$xml[$key] = $xml[$key] ? $xml[$key] : array('','','');			
			break;
		case 3:
			$key = 'About';
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			$key = 'Daysay';
			break;
		case 7:
			break;
		case 8:
			$key = 'Tools';
			$xml[$key] = $xml[$key] ? $xml[$key] : array('','','','','','','','','');			
			break;
		default:
			break;
	}
	if($_POST){$xml[$key] = $_POST[$key]; createXML($xml);}
	$smarty->_assign(array('file','xml'));
	$smarty->_display();

	function objectToArray($data)
	{
		if($data):
		$data = (array)$data;
		foreach($data as &$val)
		{
			if(is_object($val))
			{
				$val = objectToArray($val);
			}elseif($val && is_array($val)){
				foreach($val as &$v)
				{
					if(is_object($v))
					{
						$v = objectToArray($v);
					}
				}
			}
		}
		endif;
		return $data;
	}

	function createXML($data)
	{
		$xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
		$xml .= '<note>'."\n";//根节点创建
		foreach($data as $key=>$val)
		{
			if(is_array($val))
			{
				$xml .= '<'.$key.'>'."\n";
				foreach($val as $k=>$v)
				{					
					if(is_int($k))
					{
						if($k>0)$xml .= '</'.$key.'>'."\n".'<'.$key.'>'."\n";
						foreach($v as $kk=>$vv)
						{								
							$xml .= '<'.$kk.'>'.$vv.'</'.$kk.'>'."\n";								
						}						
					}else{					
						if(is_array($v))
						{
							foreach($v as $kk=>$vv)
							{
								$xml .= '<'.$k.'>'.$vv.'</'.$k.'>'."\n";
							}
						}else{
							$xml .= '<'.$k.'>'.$v.'</'.$k.'>'."\n";
						}						
					}
				}
				$xml .= '</'.$key.'>'."\n";		
			}else{
				$xml .= '<'.$key.'>'.$val.'</'.$key.'>'."\n";
			}
		}
		$xml .= '</note>';//根节点闭合
		$fp = fopen('../../one/xml/index-data.xml', "w"); //打开要写入 XML数据的文件
		fwrite($fp, $xml); //写入 XML数据
		fclose($fp); //关闭文件 
	}
?>